<?php

/**
 * Registration class.
 * Registration is the data structure for inssert data into table users and profiles
 * Author: Khanh Nguyen
 * Date: 8-3-2014 4:17pm
 */
class mRegistration extends User {

  public $id;
  public $id_profile;
  public $gender;
  public $seeking;
  public $colorHair;
  public $colorEye;
  public $tall;
  public $ethnicity;
  public $bodyType;
  public $marialStatus;
  public $childrenHave;
  public $childrenLiving;
  public $smoke;
  public $country;
  public $postalcode;
  public $city;
  public $state;
  public $dateOfBirth;
  public $userName;
  public $pass;
  public $email;
  public $greeting1;
  public $greeting2;
  public $greeting3;

  /**
   * Declares the validation rules.
   */
  public function rules() {
    return array(
        array('seeking, colorHair, colorEye, tall, ethnicity, bodyType, marialStatus, childrenHave, childrenLiving, smoke, country, dateOfBirth, userName, pass, email', 'required'),
        array('email', 'email'),
        //array('dateOfBirth', 'date', 'format'=>'yyyy-M-d'),
        array('smoke, postalcode, childrenHave, childrenLiving', 'numerical', 'integerOnly' => true),
        array('userName, pass', 'length', 'min' => 4, 'max' => 16),
        array('colorHair, colorEye, tall, ethnicity, marialStatus, smoke, country, postalcode, city, state', 'length', 'max' => 50)
    );
  }

  /**
   * Declares customized attribute labels.
   * If not declared here, an attribute would have a label that is
   * the same as its name with the first letter in upper case.
   */
  public function attributeLabels() {
    return array(
        'verifyCode' => 'Verification Code',
    );
  }

  /*
   * Alias Attribute Form into model so Validate
   */

  public function aliasAttrFormWithModel($data) {
    $purifier = new CHtmlPurifier();
    $this->seeking = $purifier->purify($data['ddlProfileResponse_999']);
    $this->colorHair = $purifier->purify($data['ddlProfileResponse_1']);
    $this->colorEye = $purifier->purify($data['ddlProfileResponse_2']);
    $this->tall = $purifier->purify($data['ddlProfileResponse_19']);
    $this->ethnicity = $purifier->purify($data['ddlProfileResponse_101']);
    $this->bodyType = $purifier->purify($data['ddlProfileResponse_4']);
    $this->marialStatus = $purifier->purify($data['ddlProfileResponse_5']);
    $this->childrenHave = $purifier->purify($data['ddlProfileResponse_16']);
    $this->childrenLiving = $purifier->purify($data['ddlProfileResponse_17']);
    $this->smoke = $purifier->purify($data['ddlProfileResponse_98']);
    $this->country = $purifier->purify($data['ddlCountry']);
    $this->postalcode = $purifier->purify(isset($data['txtPostalCode']) ? $data['txtPostalCode'] : '');
    $this->city = $purifier->purify(isset($data['txtCity']) ? $data['txtCity'] : '');
    $this->state = $purifier->purify(isset($data['txtState']) ? $data['txtState'] : '');
    $this->dateOfBirth = $purifier->purify($data['ddlBirthYear'] . '-' . $data['ddlBirthMonth'] . '-' . $data['ddlBirthDay']);
    $this->userName = $purifier->purify($data['txtUsername']);
    $this->pass = $purifier->purify($data['txtPassword']);
    $this->email = $purifier->purify($data['txtEmailAddress']);
  }

  /*
   * Alias Attribute Form into model so Validate
   */

  public function aliasAttrFormGreetingWithModel($data) {
    $purifier = new CHtmlPurifier();
    $this->id_profile = $purifier->purify($data['id_profile']);
    $this->greeting1 = $purifier->purify($data['txtGreeting1']);
    $this->greeting2 = $purifier->purify($data['txtGreeting2']);
    $this->greeting3 = $purifier->purify($data['txtGreeting3']);
  }

  public function updateGreeting(){    
    $sql = "update profiles set greeting1 = :greeting1, greeting2 = :greeting2, greeting3 = :greeting3 
              where profiles.profile_id = $this->id_profile";
    $command=db()->createCommand($sql);
    $command->bindParam(":greeting1",$this->greeting1,PDO::PARAM_STR);
    $command->bindParam(":greeting2",$this->greeting2,PDO::PARAM_STR);
    $command->bindParam(":greeting3",$this->greeting3,PDO::PARAM_STR);
    $command->execute();
  }
  /*
   * After validate normal continue validate Unique Username and Email because It's separate table Users and Profiles
   */
  public function checkUniqueUsernameEmail() {
    $rows = db()->createCommand()
            ->from('users')
            ->where('username=:username or email=:email', array(':username' => $this->userName, ':email' => $this->email))
            ->queryAll();
    if (!empty($rows)) {
      return false;
    }
    return true;
  }

  /*
   * Insert data into table Users and Profiles
   */

  public function insertUserProfile() {
    $today = date("Y-m-d H:i:s");
    $user = new User();
    $salt = $user->__generateSalt();
    $password = $user->hashPassword($this->password, $salt);
    $command = db()->createCommand();
    $command->insert('users', array(
        'username' => $this->userName,
        'password' => $password,
        'salt' => $salt,
        'email' => $this->email,
        'status' => '1',
        'seeking' => $this->seeking,
        'created_at' => $today,
        'is_online' => '0',
    ));
    $this->id = Yii::app()->db->getLastInsertID();
    $gender = array(1 => 2, 2 => 2, 3 => 1, 4 => 1);
    $this->gender = isset($gender[$this->seeking]) ? $gender[$this->seeking] : '';
    $rows = Yii::app()->db->createCommand('SELECT max(profile_id) as profile_id FROM profiles')->queryAll();
    $this->id_profile = $rows[0]['profile_id'] + 1;
    $command->insert('profiles', array(
        'profile_id' => $this->id_profile,
        'user_id' => $this->id,
        'birthdate' => $this->dateOfBirth,
        'gender' => $this->gender,
        'country' => $this->country,
        'postcode' => $this->postalcode,
        'state' => $this->state,
        'city' => $this->city,
        'seeking' => $this->seeking,
        'looking' => $this->seeking,
        'tall' => $this->tall * 2.54,
        'hair_color' => $this->colorHair,
        'eyes_color' => $this->colorEye,
        'ethnicity' => $this->ethnicity,
        'body_type' => $this->bodyType,
        'marial_status' => $this->marialStatus,
        'children' => $this->childrenHave,
        'children_living' => $this->childrenLiving,
        'smoke' => $this->smoke,
    ));
  }

  /**
   * Generates a salt that can be used to generate a password hash.
   * @return string the salt
   */
  protected function __generateSalt() {
    return uniqid('', true);
  }

}